package com.tech.zhidengxian.controller;

import com.tech.zhidengxian.model.dto.CosDTO;
import com.tech.zhidengxian.model.dto.result.ResponseCode;
import com.tech.zhidengxian.model.dto.result.ReturnResult;
import com.tech.zhidengxian.model.dto.result.ReturnResultUtils;
import com.tech.zhidengxian.service.api.CosService;
import com.tech.zhidengxian.util.FileUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private CosService cosService;

    @RequestMapping("/uploadFile")
    public ReturnResult<CosDTO> uploadFile(MultipartFile file, CosDTO cosDTO){
        CosDTO upload = FileUtil.uploadImg(file, cosDTO);
        Long ossId = cosService.uploadFile(upload);
        if (ossId>0){
            upload.setId(ossId);
            return ReturnResultUtils.returnSuccess(upload);
        }
        return ReturnResultUtils.returnFail(ResponseCode.DB_EXCEPTION,"上传异常");
    }
}
